package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.IStoreService;
import com.tarena.lbs.common.basic.converter.StoreConverter;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.param.StoreParam;
import com.tarena.lbs.pojo.basic.param.UserLocationParam;
import com.tarena.lbs.pojo.basic.param.UserStoreParam;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.MarketingSelectedQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import com.tarena.lbs.pojo.basic.query.UserStoreQuery;
import com.tarena.lbs.pojo.basic.vo.StoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "店铺管理")
public class StoreController {

    @Autowired
    private IStoreService shopService;

    @Autowired
    private StoreConverter storeConverter;

    @PostMapping("/admin/basic/store/add")
    @ApiOperation("新增店铺")
    public Result<Void> cartAdd(@RequestBody StoreParam storeParam) {
        shopService.shopAdd(storeParam);
        return Result.success();
    }

    @DeleteMapping("/admin/basic/store/delete/{id}")
    @ApiOperation("删除门店")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "门店ID", name = "id", example = "1", required = true))
    public Result<Void> deleteShop(@PathVariable Integer id) {
        shopService.deleteByID(id);
        return Result.success();
    }

    @PutMapping("/admin/basic/store/enable/{id}")
    @ApiOperation("修改门店状态-启用")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "门店ID", name = "id", example = "4", required = true))
    public Result<Void> enableStatus(@PathVariable Integer id) {
        shopService.enableStatusById(id);
        return Result.success();
    }

    @PutMapping("/admin/basic/store/disable/{id}")
    @ApiOperation("修改门店状态-禁用")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "门店ID", name = "id", example = "4", required = true))
    public Result<Void> disableStatus(@PathVariable Integer id) {
        shopService.disableStatusById(id);
        return Result.success();
    }

    @PostMapping("/admin/basic/store/getAll")
    @ApiOperation("获取门店列表-分页")
    public Result<PageResult<StoreVO>> getAll(@RequestBody StoreQuery storeQuery) {
        PageResult<StoreVO> storeVOPaginationResult = shopService.getAll(storeQuery);
        return new Result<>(storeVOPaginationResult);
    }

    @GetMapping("/admin/basic/store/{id}")
    @ApiOperation("获取门店信息-根据ID")
    @ApiImplicitParams(
            @ApiImplicitParam(value = "门店ID", name = "id", example = "4", required = true))
    public Result<StoreVO> getStoreById(@PathVariable Integer id) {
        StoreVO storeVO = shopService.getStoreById(id);
        return new Result<>(storeVO);
    }

    @PutMapping("/admin/basic/store/update")
    @ApiOperation("修改门店信息-根据ID")
    public Result updateStore(@RequestBody StoreParam storeParam) {
        shopService.updateStore(storeParam);
        return new Result<>("门店信息修改完成!");
    }

    @PostMapping("/basic/store/location")
    @ApiOperation("用户定位上报")
    public Result userLocation(@RequestBody UserLocationParam param) throws Exception {
        shopService.userLocation(param);
        return Result.success();
    }

    @PostMapping("/basic/store/interest")
    @ApiOperation("店铺关注")
    public Result userInterest(@RequestBody UserStoreParam userStoreParam) throws Exception {
        shopService.userInterest(userStoreParam);
        return Result.success();
    }

    @PostMapping("/basic/store/interest/list")
    @ApiOperation("店铺关注列表")
    public Result<PageResult<StoreVO>> userInterestStoreList(@RequestBody UserStoreQuery userStoreQuery) throws Exception {
        PageResult<StoreVO> result = shopService.userInterestStoreList(userStoreQuery);
        return new Result<>(result);
    }

    @PostMapping("/admin/basic/store/getStoreByCity")
    @ApiOperation(value = "获取城市下的店铺列表[分页]")
    public Result<PageResult<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery areaStoreQuery) {
        BasePageList<StoreBO> result = shopService.getStoreByCity(areaStoreQuery);
        return new Result<>(storeConverter.assemblePageVOS(result,areaStoreQuery));
    }

    @PostMapping("/basic/store/select/list")
    @ApiOperation("获取活动选中的店铺和店铺对应的城市")
    public Result<PageResult<StoreVO>> marketingStoreList(@RequestBody MarketingSelectedQuery query) throws Exception {
        BasePageList<StoreBO> result = shopService.marketingStoreList(query);
        return new Result<>(storeConverter.assemblePageVOS(result,query));
    }
}
